#NoTrayIcon
SetTitleMatchMode RegEx
SendMode Input

selectedText := ConcatArgs()

; locate an existing file
idx := RegExMatch(selectedText, "[a-zA-Z]:\\")
if (idx = 0)
	return ; accept only absolute path
fp := SubStr(selectedText, idx)
fp := RegExReplace(fp, "\s*$", "")
Loop {
	if (FileExist(fp))
		break
	idx := InStr(fp, "\", true, 0) ; find the right-most '\'
	if (idx = 0)
		return ; no file found
	fp := SubStr(fp, 1, idx - 1)
}

; jump to FreeCommander and focus on the file
Activate(" - FreeCommander$", "D:\install\freeCommander\freeCommander.exe")
WinWaitActive, - FreeCommander$
Sleep, 400
Send, !g

if (InStr(FileExist(fp), "D")) {
	fn := ""
	fd := fp
} else {
	SplitPath, fp, fn, fd
}

; focus directory
SendViaClipboard(fd)
Send, {ENTER}

; focus file
if (fn) {
	idx := RegExMatch(fn, "[^0-9a-zA-Z\-_ ()\[\]{}+',.]")
	if (idx > 5 || idx = 0)
		idx = 5 ; fc consumes long file name badly
	StringLeft, fn, fn, %idx%
	StringLower, fn, fn
	Sleep, 200
	Send, %fn%
	Sleep, 300
	Send, {ESC}
}

ExitApp


#include %A_ScriptDir%\..\lib\util.ahk